#include "BDZmqSender.h"

BDZmqSender::BDZmqSender(std::string address)
	: m_sAddress(address)
	, m_pSocket(NULL)
{
}


BDZmqSender::~BDZmqSender()
{
}

int BDZmqSender::Init()
{

	m_pSocket = zmq_socket(m_pContext, ZMQ_PUSH);
	if (!m_pSocket)
	{
		return -1;
	}

	int rc = zmq_bind(m_pSocket, m_sAddress.c_str());
	if (rc != 0)
	{
		printf("%s\n", zmq_strerror(errno));
		return -1;
	}
	return 0;
}

int BDZmqSender::DeInit()
{
	int rc = zmq_close(m_pSocket);
	if (rc != 0)
	{
		return -1;
	}
	return 0;
}

int BDZmqSender::SendMsg(const char* buffer, unsigned int length)
{
	zmq_msg_t msg;

	int rc = zmq_msg_init_size(&msg, length);
	void* data = zmq_msg_data(&msg);
	memcpy(data, buffer, length);

	rc = zmq_msg_send(&msg, m_pSocket, ZMQ_NOBLOCK);

	if (rc < 0)
	{
		printf("%s\n",zmq_strerror(errno));
		zmq_msg_close(&msg);
		return -1;
	}
	zmq_msg_close(&msg);

	return rc;
}
